package com.hf.servlet;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.hf.dao.StoreSettingDao;
import com.hf.entity.StoreSetting;

@WebServlet("/store.do")
public class StoreSettingServlet extends HttpServlet{
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");		
		resp.setContentType("text/html;charset=utf-8");
		resp.setCharacterEncoding("utf-8");
		StoreSettingDao storeDao=new StoreSettingDao();		
		
		String type=req.getParameter("type");
		HttpSession session=req.getSession();
		int user_id = (int) session.getAttribute("user_id");
		if("add".equals(type)){			
			StoreSetting storeSett=getRequestData(req);
			storeSett.setUser_id(user_id);
			int store_id=storeDao.saveStoreSetting(storeSett);						
			StoreSetting store=storeDao.queryStoreSetting(user_id);		
			req.setAttribute("store", store);
			
			req.getRequestDispatcher("my_shop/dian_pck.jsp").forward(req, resp);
		}else if("query".equals(type)){
			int user_id1=0;
			if(session.getAttribute("user_id")!=null){
				user_id1=(int) session.getAttribute("user_id");
			}
			StoreSetting store=storeDao.queryStoreSetting(user_id1);
			//session.setAttribute("store_id", store_id);
			req.setAttribute("store", store);
			req.getRequestDispatcher("my_shop/dian_pck.jsp").forward(req, resp);
		}
				
		
		
	}
	
	
	private StoreSetting getRequestData(HttpServletRequest req){
		
		String store_pic=req.getParameter("store_pic");
		String store_name=req.getParameter("store_name");
		String store_field=req.getParameter("store_field");		
		String store_introduction=req.getParameter("store_introduction");
		HttpSession session=req.getSession();
		int user_id=0;
		if(session.getAttribute("user_id")!=null){
			user_id=(Integer)session.getAttribute("user_id");
		}
		
		StoreSetting store=new StoreSetting();
		store.setStore_pic(store_pic);
		store.setStore_name(store_name);
		store.setStore_field(store_field);
		store.setStore_introduction(store_introduction);
		System.out.println(store_name);
		System.out.println(store_field);
		System.out.println(store_introduction);
		return store;
	}


}
